﻿using Basic.HrRemoteModel.Emp;
using Basic.OAGatewayModule.Interface;
using Basic.OAGatewayModule.Model.UserFill;
using Basic.OARemoteModel.UserFill;
using Basic.OARemoteModel.UserFill.Model;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.HttpApiGateway.Model;

namespace Basic.OAGatewayModule.lmpl
{
    internal class UserFillService : IUserFillService
    {
        public UserFillDto Get ( long id )
        {
            UserFillData data = new GetUserFillItem
            {
                Id = id
            }.Send();
            UserFillDto dto = data.ConvertMap<UserFillData, UserFillDto>();
            dto.CreateEmp = new GetEmpName
            {
                Id = data.CreateEmpId
            }.Send();
            return dto;
        }

        public PagingResult<UserFillItemDto> Query ( PagingParam<UserFillQuery> param, long empId )
        {
            UserFillItem[] items = new QueryUserFillItem
            {
                Query = new UserFillItemQuery
                {
                    QueryKey = param.Query.QueryKey,
                    EmpId = empId,
                    End = param.Query.End,
                    Begin = param.Query.Begin,
                    FillType = param.Query.FillType,
                    Status = param.Query.Status
                },
                Index = param.Index,
                Size = param.Size,
                SortName = param.SortName,
                IsDesc = param.IsDesc,
                NextId = param.NextId
            }.Send(out int count);
            if ( items.IsNull() )
            {
                return new PagingResult<UserFillItemDto>(count);
            }
            UserFillItemDto[] dtos = items.ConvertMap<UserFillItem, UserFillItemDto>();
            Dictionary<long, string> emps = new GetEmpNames
            {
                Ids = dtos.Distinct(a => a.CreateEmpId)
            }.Send();
            dtos.ForEach(a =>
            {
                a.CreateEmp = emps.GetValueOrDefault(a.CreateEmpId);
            });
            return new PagingResult<UserFillItemDto>(dtos, count);
        }
        public bool Revoke ( long id, long empId )
        {
            return new RevokeFillIn
            {
                Id = id,
                EmpId = empId
            }.Send();
        }
        public bool Submit ( long id, long empId )
        {
            return new SubmitFillIn
            {
                Id = id,
                EmpId = empId
            }.Send();
        }
    }
}
